// by Xeno
//#define __DEBUG__
#define THIS_FILE "fn_initvec.sqf"
#include "x_setup.sqf"
// TYRGHEN -- Added the resetting of the marker. If the vehicle respawns, even if the marker exist, we need to reset it.
#define __vecmarker _mxname = _car select 2; \
if (str(markerPos _mxname) == "[0,0,0]") then { \
[_mxname, getPosASL _vec,"ICON",_car select 4,[1,1],_mt,0,_car select 3] call FUNC(CreateMarkerLocal);\
} else { \
_mxname setMarkerPosLocal (visiblePositionASL _vec); \
_mxname setMarkerTypeLocal (_car select 3); \
_mxname setMarkerTextLocal _mt; \
_mxname setMarkerColorLocal (_car select 4); \
_mxname setMarkerSizeLocal [1,1]; \
}; \
_vec setVariable [QGVAR(marker), _mxname];\
GVAR(marker_vecs) set [count GVAR(marker_vecs), _vec];

// TYRGHEN -- Added the resetting of the marker. If the vehicle respawns, even if the marker exist, we need to reset it.
#define __chopmarker _mxname = _car select 2; \
if (str(markerPos _mxname) == "[0,0,0]") then { \
[_mxname, getPosASL _vec,"ICON",_car select 5,[1,1],_car select 6,0,_car select 4] call FUNC(CreateMarkerLocal);\
} else { \
_mxname setMarkerPosLocal (visiblePositionASL _vec); \
_mxname setMarkerTypeLocal (_car select 4); \
_mxname setMarkerTextLocal (_car select 6); \
_mxname setMarkerColorLocal (_car select 5); \
_mxname setMarkerSizeLocal [1,1]; \
}; \
_vec setVariable [QGVAR(marker), _mxname];\
GVAR(marker_vecs) set [count GVAR(marker_vecs), _vec];\
if (count _car > 8) then {_vec setVariable [QGVAR(lift_types), _car select 8]}

#define __chopset _index = _car select 1;\
_vec setVariable [QGVAR(choppertype), _index];\
_vec setVariable [QGVAR(vec_type), "chopper"];\
switch (_index) do {\
	case 0: {_vec addEventHandler ["getin", {[_this,0] call FUNC(checkhelipilot)}]};\
	case 1: {_vec addEventHandler ["getin", {_this call FUNC(checkhelipilot_wreck)}]};\
	case 2: {_vec addEventHandler ["getin", {[_this,1] call FUNC(checkhelipilot)}]};\
};\
_vec addEventHandler ["getout", {_this call FUNC(checkhelipilotout)}]

#define __sidew _vec setVariable [QGVAR(side), blufor]
#define __sidee _vec setVariable [QGVAR(side), opfor]
#define __vecname _vec setVariable [QGVAR(vec_name), _car select 6]
#define __chopname _vec setVariable [QGVAR(vec_name), _car select 7]
#define __checkenterer _vec addEventHandler ["getin", {_this call FUNC(checkenterer)}]
#define __pvecs {if ((_x select 1) == _d_vec) exitWith {_car = _x}} forEach GVAR(p_vecs)
#define __pvecss(sname) {if ((_x select 1) == _d_vec) exitWith {_car = _x}} forEach d_p_vecs_##sname

#define __staticl \
_vec addAction[(localize "STR_DOM_MISSIONSTRING_256") call FUNC(GreyText),{_this spawn FUNC(load_static)},_d_vec,-1,false];\
_vec addAction[(localize "STR_DOM_MISSIONSTRING_257") call FUNC(RedText),{_this spawn FUNC(unload_static)},_d_vec,-1,false]

#define __addchopm _vec addAction [(localize "STR_DOM_MISSIONSTRING_258") call FUNC(GreyText), {_this spawn FUNC(vecdialog)},[],-1,false]
#define __halo _vec addAction [(localize "STR_DOM_MISSIONSTRING_259") call FUNC(GreyText), {_this call FUNC(chalo)},[],-1,false,true,"","vehicle player != player && {((vehicle player) call d_fnc_GetHeight) > 50}"]
//TYRGHEN
#define __addvecm _vec addAction [(localize "STR_DOM_MISSIONSTRING_2000") call FUNC(GreyText),{_this spawn FUNC(vecdialog)},[],-1,false]

#define __chopdoors \
_dooranims = []; \
_conf = configFile/"CfgVehicles"/(typeOf _vec)/"AnimationSources"; \
for "_i" from 0 to (count _conf - 1) do { \
	_subc = _conf select _i; \
	if (getText(_subc/"source") == "door") then { \
		_dooranims set [count _dooranims, configName _subc]; \
	}; \
}; \
if (count _dooranims > 0) then { \
	_vec addAction ["<t color='#CFCFCF'>" + (localize "STR_DOM_MISSIONSTRING_1507") + "</t>", {_this call FUNC(chopperdoors)},[1, _dooranims] ,6,false,true,"", format ["alive _target && {_target animationPhase '%1' == 0}", _dooranims select 0]]; \
	_vec addAction ["<t color='#CFCFCF'>" + (localize "STR_DOM_MISSIONSTRING_1508") + "</t>", {_this call FUNC(chopperdoors)}, [0, _dooranims],6,false,true,"", format ["alive _target && {_target animationPhase '%1' == 1}", _dooranims select 0]]; \
}

private ["_vec","_vec_varname","_desm","_d_team_reserved","_vmdp"];

_vec = _this;

private "_desm";
_desm = _vec getVariable [QGVAR(deserted_marker), ""];
if (_desm != "") then {
	[_desm, getPosASL _vec,"ICON","ColorBlack",[1,1], format [(localize "STR_DOM_MISSIONSTRING_260"), [typeOf _vec, 0] call FUNC(GetDisplayName)],0,"hd_dot"] call FUNC(CreateMarkerLocal);
};

_d_vec = GV(_vec,GVAR(vec));
if (isNil "_d_vec") exitWith {};

// TYRGHEN
_d_team_reserved = GV(_vec,GVAR(team_reserved));
if (!isnil "_d_team_reserved" && {_d_team_reserved} && {!isnil "mp_rights_fnc_hasRoles"}) then {
	// diag_log Format["Driver Check add: %1", [vehiclevarname _vec, typeof _vec]];
	switch (true) do {
		case (_vec iskindof "Air"): {
			_vec addEventhandler ["GetIn", {[_this,["pilot","admin"],["DRIVER"]] call mp_rights_fnc_crewCheck}];
		};
		case (_vec iskindof "Tank"): {
			_vec addEventhandler ["GetIn", {[_this,["crew","admin"],["DRIVER","GUNNER"]] call mp_rights_fnc_crewCheck}];
		};
		case (_vec iskindof "AllVehicles"): {
			_vec addEventhandler ["GetIn", {[_this,[]] call mp_rights_fnc_crewCheck}];
		};
	};
};
// TYRGHEN
if (!isnil "mp_rights_fnc_hasRoles") then {
	if ([(getPlayerUID player),["member","admin","eject"]] call mp_rights_fnc_hasRoles) then {
		_vec addAction [(localize "STR_DOM_MISSIONSTRING_2004") call FUNC(GreyText),"x_client\x_ejectunits.sqf",[], 0, true, true, "", "alive _target && (vehicle player == player) && (count (crew _target) > 0) && (speed _target <= 0.3)"];
	};
};
// TYRGHEN
if (isNil QGVAR(with_carrier)) then {
	_vec addEventHandler ["Fired",FUNC(KickPlayerBaseFiredVehicle)];
};
//TYRGHEN
_vec_varname = GV(_vec,GVAR(vec_varname));
if (!isnil "_vec_varname") then {
	_vec setVehicleVarName _vec_varname;
	_vec call compile format ["%1=_vec;",_vec_varname];
};

if (!isNil {GV(_vec,GVAR(vcheck))}) exitWith {};
_vec setVariable [QGVAR(vcheck), true];

if (_d_vec < 100) exitWith {
	_car = [];
#ifndef __TT__
	__pvecs;
#else
	__pvecss(blufor);
#endif
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		_mt = _car select 5;
		_vmdp = GV(_vec,GVAR(MHQ_Deployed));
		if (!isNil "_vmdp" && {_vmdp}) then {
			_mt = format [(localize "STR_DOM_MISSIONSTRING_261"), _mt];
		};
		if (str(markerPos (_car select 2)) != "[0,0,0]") then {
			(_car select 2) setMarkerTextLocal _mt;
		};
		__vecmarker;
		_vec setVariable [QGVAR(marker_text), _car select 5];
#ifdef __TT__
		};
#endif
		__vecname;
	};
	if (!alive _vec) exitWith {};
#ifdef __TT__
	if (GVAR(player_side) == blufor) then {
#endif
	_vec addAction [(localize "STR_DOM_MISSIONSTRING_262") call FUNC(GreyText), {_this spawn FUNC(vecdialog)},_d_vec,-1,false];
#ifdef __TT__
	};
#endif
	_vec setVariable [QGVAR(vec_type), "MHQ"];
#ifdef __TT__
	_vec addEventHandler ["getin", {_this call FUNC(checkdriver)}];
	__sidew;
#endif
	_vec setAmmoCargo 0;
	
	//TYRGHEN
	_vec addAction [("<t color=""#ED2744"">") + (localize "STR_DOM_MISSIONSTRING_2002") + "</t>","x_client\x_healatmhq.sqf",[], 10000, true, true, "", "alive _target && alive player && (vehicle player == player) && ((isBleeding player) || (damage player) > 0)"];
};

if (_d_vec < 200) exitWith {
	_car = [];
#ifndef __TT__
	__pvecs;
#else
	__pvecss(blufor);
#endif
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		_mt = _car select 5;
		__vecmarker;
#ifdef __TT__
		};
#endif
		__vecname;
	};
	if (!alive _vec) exitWith {};
#ifdef __TT__
	__sidew;
	__checkenterer;
#endif
	_vec setAmmoCargo 0;
};

if (_d_vec < 300) exitWith {
	_car = [];
#ifndef __TT__
	__pvecs;
#else
	__pvecss(blufor);
#endif
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		_mt = _car select 5;
		__vecmarker;
#ifdef __TT__
		};
#endif
		__vecname;
	};
	if (!alive _vec) exitWith {};
#ifdef __TT__
	__sidew;
	__checkenterer;
#endif
	_vec setAmmoCargo 0;
};

if (_d_vec < 400) exitWith {
	_car = [];
#ifndef __TT__
	__pvecs;
#else
	__pvecss(blufor);
#endif
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		_mt = _car select 5;
		__vecmarker;
#ifdef __TT__
		};
#endif
		__vecname;
	};
	if (!alive _vec) exitWith {};
#ifndef __TT__
	if (GVAR(with_ai) || {GVAR(with_ai_features) == 0} || {GVAR(string_player) in GVAR(is_engineer)}) then {
#else
	if (GVAR(player_side) == blufor && {GVAR(string_player) in GVAR(is_engineer)}) then {
#endif
		__staticl;
	} else {
		_vec addEventHandler ["getin", {_this call FUNC(checktrucktrans)}];
	};
	_vec setVariable [QGVAR(vec_type), "Engineer"];
#ifdef __TT__
	__sidew;
#endif
	_vec setAmmoCargo 0;
};

if (_d_vec < 500) exitWith {
	_car = [];
#ifndef __TT__
	__pvecs;
#else
	__pvecss(blufor);
#endif
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		_mt = _car select 5;
		__vecmarker;
#ifdef __TT__
		};
#endif
		__vecname;
	};
	if (!alive _vec) exitWith {};
#ifdef __TT__
	__sidew;
	__checkenterer;
#endif
	_vec setAmmoCargo 0;
	// TYRGHEN
	if (_d_team_reserved && ([(getPlayerUID player)] call mp_rights_fnc_isRecruit)) then {
		__addvecm;
	};
};

#ifdef __TT__
if (_d_vec < 1100) exitWith {
	_car = [];
	__pvecss(opfor);
	_eeit = false;
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		if (GVAR(player_side) == opfor) then {
			_mt = _car select 5;
			_vmdp = GV(_vec,GVAR(MHQ_Deployed));
			if (!isNil "_vmdp" && {_vmdp}) then {
				_mt = format [(localize "STR_DOM_MISSIONSTRING_261"), _mt];
			};
			if (str(markerPos (_car select 2)) != "[0,0,0]") then {
				(_car select 2) setMarkerTextLocal _mt;
			};
			__vecmarker;
			_vec setVariable [QGVAR(marker_text), _car select 5];
		};
		__vecname;
	};
	if (!alive _vec || {_eeit}) exitWith {};
	if (GVAR(player_side) == opfor) then {
		_vec addAction [(localize "STR_DOM_MISSIONSTRING_262") call FUNC(GreyText), {_this spawn FUNC(vecdialog)},_d_vec,-1,false];
	};
	_vec setVariable [QGVAR(vec_type), "MHQ"];
	__sidee;
	_vec addEventHandler ["getin", {_this call FUNC(checkdriver)}];
	_vec setAmmoCargo 0;
};

if (_d_vec < 1200) exitWith {
	_car = [];
	__pvecss(opfor);
	_eeit = false;
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		if (GVAR(player_side) == opfor) then {
			_mt = _car select 5;
			__vecmarker;
		};
		__vecname;
	};
	if (!alive _vec || {_eeit}) exitWith {};
	__sidee;
	__checkenterer;
	_vec setAmmoCargo 0;
};

if (_d_vec < 1300) exitWith {
	_car = [];
	__pvecss(opfor);
	_eeit = false;
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		if (GVAR(player_side) == opfor) then {
			_mt = _car select 5;
			__vecmarker;
		};
		__vecname;
	};
	if (!alive _vec || {_eeit}) exitWith {};
	__sidee;
	__checkenterer;
	_vec setAmmoCargo 0;
};

if (_d_vec < 1400) exitWith {
	_car = [];
	__pvecss(opfor);
	_eeit = false;
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		if (GVAR(player_side) == opfor) then {
			_mt = _car select 5;
			__vecmarker;
		};
		__vecname;
	};
	if (!alive _vec || {_eeit}) exitWith {};
	if (GVAR(player_side) == opfor && {GVAR(string_player) in GVAR(is_engineer)}) then {
		__staticl;
	};
	_vec setVariable [QGVAR(vec_type), "Engineer"];
	__sidee;
	_vec addEventHandler ["getin", {_this call FUNC(checktrucktrans)}];
	_vec setAmmoCargo 0;
};

if (_d_vec < 1500) exitWith {
	_car = [];
	__pvecss(opfor);
	_eeit = false;
	if (count _car > 0) then {
		__mNsSetVar [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		if (GVAR(player_side) == opfor) then {
			_mt = _car select 5;
			__vecmarker;
		};
		__vecname;
	};
	if (!alive _vec || {_eeit}) exitWith {};
	__sidee;
	__checkenterer;
	_vec setAmmoCargo 0;
};
#endif

if (_d_vec < 4000) exitWith {
	_car = [];
#ifndef __TT__
	{if ((_x select 3) == _d_vec) exitWith {_car = _x}} forEach GVAR(choppers);
#else
	{if ((_x select 3) == _d_vec) exitWith {_car = _x}} forEach GVAR(choppers_blufor);
#endif
	__TRACE_1("_car","_car")
	if (count _car > 0) then {
		if (!alive _vec) exitWith {};
		__mNsSetVar [_car select 0, _vec];
		__chopname;
#ifdef __TT__
		if (GVAR(player_side) == blufor) then {
#endif
		__chopmarker;
#ifdef __TT__
		};
#endif
	};
	if (!alive _vec) exitWith {};
#ifdef __TT__
	if (GVAR(player_side) == blufor) then {
#endif
	// TYRGHEN
	if (_d_team_reserved) then {
		if ([(getPlayerUID player)] call mp_rights_fnc_isRecruit) then { __addvecm; };
	} else {
		__addchopm;
	};
#ifdef __TT__
	};
	__sidew;
#endif
	__chopset;
#ifdef __TT__
	if (GVAR(player_side) == blufor) then {
#endif
	__halo;
#ifdef __TT__
	};
#endif
	__chopdoors;
};

#ifdef __TT__
if (_d_vec < 5000) exitWith {
	_car = [];
	{if ((_x select 3) == _d_vec) exitWith {_car = _x}} forEach GVAR(choppers_opfor);
	__TRACE_1("_car","_car")
	if (count _car > 0) then {
		if (!alive _vec) exitWith {};
		__mNsSetVar [_car select 0, _vec];
		__chopname;
		if (GVAR(player_side) == opfor) then {__chopmarker};
	};
	if (!alive _vec) exitWith {};
	if (GVAR(player_side) == opfor) then {__addchopm};
	__chopset;
	if (GVAR(player_side) == opfor) then {
		__halo;
	};
	__chopdoors;
	__sidee;
};
#endif